package com.laiyima.zhongjiang.linghuilv.demo.me;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.laiyima.zhongjiang.linghuilv.demo.R;
import com.laiyima.zhongjiang.linghuilv.demo.home.NameActivity;
import com.laiyima.zhongjiang.linghuilv.demo.util.CircleImageView;

import org.xutils.view.annotation.ViewInject;

import java.io.File;


public class UserActivity extends AppCompatActivity implements View.OnClickListener{
    private String TAG="UserActivity";
    private int uesrCode;
    private Button mButton;
    @ViewInject(R.id.user_touxiang)
    private CircleImageView set_head_image;


    private ImageView toSetting;
    private String[] photoArry = new String[]{"相册", "相机"};
    private LinearLayout toVersion,toAboutUs,user_toName,user_tobankcard;

    private UserActivity mContext;
    private TextView showName,show_version;


    private static final int TIME_EXIT=3000;
    private static int isExit=0;
    //实现按两次后退才退出
    private CountDownTimer mCountDownTimer = new CountDownTimer(TIME_EXIT,1000) {
        @Override
        public void onTick(long l) {
            isExit++;

        }

        @Override
        public void onFinish() {
            isExit=0;
        }
    };

    public boolean onKeyDown(int keyCode, KeyEvent event){
        if(keyCode==KeyEvent.KEYCODE_BACK){
            exit();
            return true;
        }
        return false;
    }
    private void exit(){
        if(isExit<1){
            Toast.makeText(getApplicationContext(),"再点击一次返回退出程序",Toast.LENGTH_SHORT).show();
            mCountDownTimer.start();
        }else{
            isExit=0;
            Toast.makeText(getApplicationContext(),"退出程序",Toast.LENGTH_SHORT).show();
            mCountDownTimer.cancel();
            Intent startMain = new Intent(Intent.ACTION_MAIN);
            startMain.addCategory(Intent.CATEGORY_HOME);
            startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(startMain);
            System.exit(0);
        }
    }

    public static synchronized String getVersionCode(Context context) {
        try {
            PackageManager packageManager = context.getPackageManager();
            PackageInfo packageInfo = packageManager.getPackageInfo(
                    context.getPackageName(), 0);
            return packageInfo.versionName;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user);
        toVersion=findViewById(R.id.user_toVersion);
        toAboutUs=findViewById(R.id.to_aboutUs);
        toSetting=findViewById(R.id.to_setting);
        set_head_image=(CircleImageView)findViewById(R.id.user_touxiang);
        show_version=findViewById(R.id.show_version);
        showName=findViewById(R.id.shoe_user_name);
        user_tobankcard=findViewById(R.id.user_tobankcard);
        user_toName=findViewById(R.id.user_toName);
        user_toName.setOnClickListener(this);
        toVersion.setOnClickListener(this);
        toAboutUs.setOnClickListener(this);
        toSetting.setOnClickListener(this);
        set_head_image.setOnClickListener(this);
        user_tobankcard.setOnClickListener(this);



        SharedPreferences sharedPreferences =getSharedPreferences("userinfo.txt", Context.MODE_PRIVATE);
        String name = sharedPreferences.getString("name", "");
        showName.setText(name);




        String version=getVersionCode(this);
        show_version.setText("version   "+version);



        showIcon();
    }
    @Override
    protected void onResume() {
        super.onResume();
        showIcon();
    }
    private void showIcon() {
        //String icon_path = SpUtil.getString(mContext, GlobalVariables.USER_ICON_FILE_PATH);
        SharedPreferences sharedPreferences =getSharedPreferences("userinfo.txt", Context.MODE_PRIVATE);
        String icon_path = sharedPreferences.getString("headimagepath", "");
        if (icon_path != null && isFileExist(icon_path)) {
            Bitmap bitmap = BitmapFactory.decodeFile(icon_path);
            set_head_image.setImageBitmap(bitmap);
        }

    }
    public static boolean isFileExist(String icon_path) {
        File file = new File(icon_path);
        if (file.exists()) {
            return true;
        }
        return false;
    }


    @Override
    public void onClick(View v) {
        int id=v.getId();
        switch (id){
            case R.id.user_tobankcard:
                Intent ubintent=new Intent(UserActivity.this,BindingBankCardActivity.class);
                startActivity(ubintent);
                break;
            case R.id.user_toVersion:
                Intent Versionintent=new Intent(UserActivity.this,VersionActivity.class);
                startActivity(Versionintent);
                break;
            case R.id.to_aboutUs:
                Intent AboutUsintent=new Intent(UserActivity.this,AboutUsActivity.class);
                startActivity(AboutUsintent);
                break;


            case R.id.to_setting:
                Intent Settingintent=new Intent(UserActivity.this,UserInfoActivity.class);
                startActivity(Settingintent);
                break;
            case R.id.user_touxiang:
                Intent Settinintent=new Intent(UserActivity.this,SettingActivity.class);
                startActivity(Settinintent);
                break;
            case R.id.user_toName:
                Intent UNintent=new Intent(UserActivity.this, NameActivity.class);
                startActivity(UNintent);
                break;
        }

    }
}
